import requests,IPy,re
iplist=open("ip.txt").read()#从文本读取ip列表
iplist=re.findall("\d+\.\d+\.\d+\.\d+/?\d{0,2}",iplist)#正则处理ip
with open("res.txt","w") as output:
    for ipx in iplist:
        try:
            ip = IPy.IP(ipx).strNormal(3)  # ip格式转换为首尾模式
            ip = str(ip).split("-")  # 取网段头尾2个地址
            # ip = "113.215.2.220"
            for x in ip:  # 调用taobao ip库查询
                r = requests.get(url="http://ip.taobao.com//service/getIpInfo.php?ip=" + ip[0])
                res = r.json()["data"]["isp"]
                stat = "OK"
                if '华通' not in res and '华数' not in res:  # 判断是否为华数或华通的地址，修改对应的状态
                    stat = "ERROR"
                    break
        except Exception as e:
            res = str(e)  # 输出异常信息
            stat = "ERROR"
        print(ipx, res, stat)
        output.write("%s\t%s\t%s\n" % (ipx, res, stat))


